﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using GomMinhLongDO;
using GomMinhLongBL;

namespace GomMinhLong
{
    public partial class Products : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            load();
        }

        public void load()
        {

            Page.Title = "Sản phẩm" + " - " + new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue;
            DataTable dt = new DataTable();
            if (Request.QueryString["ID"] != null)
            {
                if (Request.QueryString["ID"] == "All")
                {
                    dt = new cmsSanPhamBL().SelectAll();
                }
                else
                {
                    dt = new cmsSanPhamBL().SelectLoaiSanPham(int.Parse(Request.QueryString["ID"].ToString()));
                    Page.Title = new cmsCategoryBL().Select(new cmsCategoryDO { CategoryID = int.Parse(Request.QueryString["ID"].ToString()) }).Title + Ultility.PageTitle();
                }
            }
            else
            {
                dt = new cmsSanPhamBL().SelectAll();
            }
            bindatalist(dt);
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }

        protected void ddlPhanLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DataTable dtPhanLoai = new cmsSanPhamBL().cmsSanPham_SelectByPhanLoai(int.Parse(ddlPhanLoai.SelectedValue));

            //bindatalist(dtPhanLoai);

        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            rptSanPham.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 15;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rptSanPham.Visible = true;
                rptSanPham.DataSource = objPds;
                rptSanPham.DataBind();
            }
            else
            {
                lbThongbao.Visible = true;
                lbThongbao.Text = "Không có sản phẩm!";
            }
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            load();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            load();
        }
        public string WordCut(string text)
        {
            return Ultility.WordCut(text, 100, new char[] { ' ', '.', ',', ';' }) + "...";

        }
        public string WordCutTitle(string text)
        {
            return Ultility.WordCut(text, 45, new char[] { ' ', '.', ',', ';' }) + "";

        }
    }
}